package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.dto.VerificationCodeDTO;
import com.zhuanzhuan.hero.user.dto.auth.CommitAuthDTO;
import com.zhuanzhuan.hero.user.dto.auth.QueryUserAuthDetailDTO;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.UserAuthTypeEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.helper.VerificationCodeHelper;
import com.zhuanzhuan.hero.user.service.HeroUserAuthenticationService;
import com.zhuanzhuan.hero.user.service.HeroUserInfoService;
import com.zhuanzhuan.hero.user.util.Assert;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.auth.UserAuthDetailVO;
import com.zhuanzhuan.hero.user.vo.auth.UserAuthResultVO;
import com.zhuanzhuan.hero.user.vo.auth.UserAuthStateVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName UserAuthenticationFacadeImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/27 19:33
 */
@ServiceBehavior
@Slf4j
public class UserAuthenticationFacadeImpl implements IUserAuthenticationFacade {

    @Resource
    private HeroUserAuthenticationService userAuthService;

    @Resource
    private HeroUserInfoService userInfoService;

    @Resource
    private VerificationCodeHelper verificationCodeHelper;

    @ZZMethod(value = "queryUserAuthStates", desc = "查询用户认证状态", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<List<UserAuthStateVO>> queryUserAuthStates(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userAuthService.queryUserAuthStates(ServiceHeaderUtil.parseUid(header)));
    }

    @ZZMethod(value = "isRealnameAuth", desc = "是否已实名认证", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Integer> isRealnameAuth(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userAuthService.isRealnameAuth(ServiceHeaderUtil.parseUid(header)));
    }

    @Override
    public Result<Boolean> isRealnameAuthScf(@NotNull Long uid) {
        return Result.success(NumConsts.ONE.equals(userAuthService.isRealnameAuth(uid)));
    }

    @Override
    public Result<List<UserAuthStateVO>> queryUserAuthStates(Long uid) {
        return Result.success(userAuthService.queryUserAuthStates(uid));
    }

    @ZZMethod(value = "queryUserAuthDetail", desc = "查询实人、企业认证详情", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserAuthDetailVO> queryUserAuthDetail(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid QueryUserAuthDetailDTO queryUserAuthDetailDTO) {
        return ZZOpenScfBaseResult.buildSucc(userAuthService.queryUserAuthDetail(ServiceHeaderUtil.parseUid(header),
            queryUserAuthDetailDTO.getAuthType()));
    }

    @ZZMethod(value = "commitUserAuth", desc = "提交认证审核", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserAuthResultVO> commitUserAuth(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid CommitAuthDTO commitAuthDTO) {

        Integer authType = commitAuthDTO.getAuthType();
        //二次校验验证码
        if (UserAuthTypeEnum.BM_WAREHOUSE.getCode().equals(authType)
            || UserAuthTypeEnum.ZHUANZHUAN_MERCHANT.getCode().equals(authType)) {
            VerificationCodeDTO verificationCode = commitAuthDTO.getVerificationCode();
            if (Objects.isNull(verificationCode)) {
                throw new BusinessException(ExceptionMsgEnum.PARAM_ERROR);
            }
            boolean verificationResult =
                verificationCodeHelper.verificationCode(commitAuthDTO.getBindMobile(), verificationCode.getMkvAction(),
                    verificationCode.getMkvToken());
            Assert.isTrue(verificationResult, ExceptionMsgEnum.VERFICATION_CODE_ERROR);
        }

        Long uid = ServiceHeaderUtil.parseUid(header);
        userInfoService.checkCanEdit(uid);

        return ZZOpenScfBaseResult.buildSucc(userAuthService.commitUserAuth(uid, commitAuthDTO));
    }
}
